package com.example.fresheveryday.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.fresheveryday.po.CartVo;
import com.example.fresheveryday.po.Product;
import com.example.fresheveryday.po.Shopcart;
import com.example.fresheveryday.restful.JsonResult;
import com.example.fresheveryday.service.CartVoService;
import com.example.fresheveryday.service.ProductService;
import com.example.fresheveryday.service.ShopcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2023-07-5
 */
@RestController
@RequestMapping("/shopcart")
public class ShopcartController {
    @Autowired
    ShopcartService shopcartService;
    @Autowired
    CartVoService cartVoService;
    @Autowired
    ProductService productService;
    @RequestMapping("/myList")
    public List<CartVo> list(@RequestBody Map<String,Object> map){
        String id = map.get("id").toString();
        return cartVoService.getListByUser(id);
    }
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(@RequestBody(required = false) Shopcart shopcart){
        System.out.println(shopcart);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",shopcart.getProductid());
        Product product = productService.getOne(wrapper);
        if(product.getQuantity() == 0){
            return JsonResult.error("库存不足");
        }
        shopcartService.save(shopcart);
        return JsonResult.success("success");
    }
    @RequestMapping("/update")
    public JsonResult update(@RequestBody(required = false) Shopcart shopcart){
        System.out.println(shopcart);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",shopcart.getId());
        shopcartService.update(shopcart,wrapper);
        return JsonResult.success("success");
    }
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody(required = false) String id){
        System.out.println("前端商品id" + id);
        shopcartService.removeById(id);
        return JsonResult.success("success");
    }


}
